<script setup lang="ts">
import { computed } from 'vue';

interface Props {
  /** 卡片标题 */
  title?: string;
  /** 统计数值 */
  value?: string | number;
  /** 描述信息 */
  desc?: string;
  /** 卡片类型 */
  type?: 'primary' | 'success' | 'warning' | 'error' | 'default';
  /** 自定义背景色 */
  backgroundColor?: string;
  /** 自定义文字颜色 */
  textColor?: string;
  /** 自定义类名 */
  customClass?: string;
}

const props = withDefaults(defineProps<Props>(), {
  title: '',
  value: '',
  desc: '',
  type: 'default',
  backgroundColor: '',
  textColor: '',
  customClass: ''
});

const cardClass = computed(() => {
  const baseClass = 'stat-card';
  const typeClass = props.type !== 'default' ? `stat-card-${props.type}` : '';
  return `${baseClass} ${typeClass} ${props.customClass}`;
});

const cardStyle = computed(() => {
  const style: Record<string, string> = {};

  if (props.backgroundColor) {
    style.background = props.backgroundColor;
  }

  if (props.textColor) {
    style.color = props.textColor;
  }

  return style;
});
</script>

<template>
  <div :class="cardClass" :style="cardStyle">
    <div v-if="$slots.icon" class="stat-icon">
      <slot name="icon" />
    </div>

    <div class="stat-content">
      <div class="stat-title">
        <slot name="title">{{ title }}</slot>
      </div>

      <div class="stat-value">
        <slot name="value">{{ value }}</slot>
      </div>

      <div v-if="$slots.desc || desc" class="stat-desc">
        <slot name="desc">{{ desc }}</slot>
      </div>
    </div>

    <div v-if="$slots.extra" class="stat-extra">
      <slot name="extra" />
    </div>
  </div>
</template>

<style scoped>
.stat-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  padding: 24px;
  color: white;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 120px;
  width: 100%;
  box-sizing: border-box;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4);
}

.stat-icon {
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  font-size: 24px;
}

.stat-content {
  flex: 1;
  min-width: 0;
}

.stat-title {
  font-size: 14px;
  opacity: 0.9;
  margin-bottom: 8px;
  font-weight: 500;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 4px;
  line-height: 1.2;
}

.stat-desc {
  font-size: 12px;
  opacity: 0.7;
  line-height: 1.4;
}

.stat-extra {
  flex-shrink: 0;
  margin-left: auto;
}

/* 卡片类型变体 */
.stat-card-primary {
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.stat-card-primary:hover {
  box-shadow: 0 8px 24px rgba(24, 144, 255, 0.4);
}

.stat-card-success {
  background: linear-gradient(135deg, #52c41a 0%, #389e0d 100%);
  box-shadow: 0 4px 12px rgba(82, 196, 26, 0.3);
}

.stat-card-success:hover {
  box-shadow: 0 8px 24px rgba(82, 196, 26, 0.4);
}

.stat-card-warning {
  background: linear-gradient(135deg, #faad14 0%, #d48806 100%);
  box-shadow: 0 4px 12px rgba(250, 173, 20, 0.3);
}

.stat-card-warning:hover {
  box-shadow: 0 8px 24px rgba(250, 173, 20, 0.4);
}

.stat-card-error {
  background: linear-gradient(135deg, #ff4d4f 0%, #cf1322 100%);
  box-shadow: 0 4px 12px rgba(255, 77, 79, 0.3);
}

.stat-card-error:hover {
  box-shadow: 0 8px 24px rgba(255, 77, 79, 0.4);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stat-card {
    padding: 16px;
    min-height: 100px;
    gap: 12px;
  }

  .stat-icon {
    width: 40px;
    height: 40px;
    font-size: 20px;
  }

  .stat-value {
    font-size: 24px;
  }

  .stat-title {
    font-size: 13px;
  }

  .stat-desc {
    font-size: 11px;
  }
}

@media (max-width: 480px) {
  .stat-card {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }

  .stat-extra {
    margin-left: 0;
    margin-top: 8px;
  }
}

/* 暗色主题适配 */
.dark .stat-card {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.dark .stat-card:hover {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
</style>
